package com.example.javafx09;

import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;
import javafx.util.Duration;

import java.io.File;
import java.net.MalformedURLException;

public class HelloController {
    @FXML
    private MediaView mediaView;

    @FXML
    private Pane mediaBorder;

    @FXML
    private Button operationBtn;

    @FXML
    private Slider timeSlider;

    private MediaPlayer player;

    @FXML
    private Label timeLabel;

    private String total;

    private int totalTime;


    @FXML
    public void initialize(){

        timeSlider.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) {
                double value = t1.doubleValue();
                if (value == player.getCurrentTime().toSeconds()) {
                    return;
                }
                System.out.println(value);
                player.seek(Duration.seconds(value));
            }
        });
    }

    @FXML
    public void selectVideo() {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setInitialDirectory(new File("C:\\Users\\ohq\\Desktop"));
        fileChooser.setSelectedExtensionFilter(new FileChooser.ExtensionFilter("mp4 file", "*.mp4"));
        File file = fileChooser.showOpenDialog(null);
        if (file != null) {
            System.out.println(file.getAbsolutePath());
            Media media = null;
            try {
                media = new Media(file.toURI().toURL().toString());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            player = new MediaPlayer(media);

            player.setOnReady(new Runnable() {
                @Override
                public void run() {
                    Duration duration = player.getMedia().getDuration();
                    int height = player.getMedia().getHeight();
                    int width = player.getMedia().getWidth();
                    System.out.println(height);
                    System.out.println(width);
                    mediaView.setFitHeight(height * 0.5);
                    mediaView.setFitWidth(width * 0.5);
                    mediaBorder.setPrefHeight(height * 0.51);
                    mediaBorder.setPrefWidth(width * 0.51);
                    mediaBorder.setStyle("-fx-border-color: black");
                    timeSlider.setMax(duration.toSeconds());
                    totalTime = (int) duration.toSeconds();
                    timeLabel.setText("00:00/" + timeFormat(totalTime));
                }
            });

            player.currentTimeProperty().addListener(new InvalidationListener() {
                @Override
                public void invalidated(Observable observable) {
                    System.out.println(player.getCurrentTime());
                    timeSlider.setValue(player.getCurrentTime().toSeconds());
                    timeLabel.setText(timeFormat((int) player.getCurrentTime().toSeconds()) + "/" +timeFormat(totalTime));
                }
            });

            mediaView.setMediaPlayer(player);
        }
    }

    @FXML
    public void play() {
        if (player == null) {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setHeaderText("暂未加载任何视频");
            alert.show();
        } else {
            if (operationBtn.getText().equals("播放")) {
                player.play();
                operationBtn.setText("暂停");
            } else {
                player.pause();
                operationBtn.setText("播放");
            }

        }

    }

    private String timeFormat(int second) {
        int minute = second / 60;
        int seconds = second % 60;
        String miniteStr = "";
        if (minute < 10) {
            miniteStr = "0" + minute;
        } else {
            miniteStr = minute + "";
        }

        String secondStr = "";
        if (seconds < 10) {
            secondStr = "0" + seconds;
        } else {
            secondStr = seconds + "";
        }
        return miniteStr + ":" + secondStr;
    }
}